package calculation;

import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;

public class CalculatorForKolokvijum {
	
	ArrayList<Double> x2 = new ArrayList<Double>();
	ArrayList<Double> x = new ArrayList<Double>();
	String [] s1;
	
	
	public String getTheSameX(String function) throws NumberFormatException{
		s1 = function.split("\\+");
		for (int i = 0; i < s1.length; i++) {
			if(s1[i].contains("x^2")){
				x2.add(Double.parseDouble(s1[i].substring(0,s1[i].indexOf('*'))));
			} else if(s1[i].contains("x")) {
				x.add(Double.parseDouble(s1[i].substring(0,s1[i].indexOf('*'))));
			} else {
				throw new NumberFormatException();
			}
		}
		double a =add(x2) /  add(x);
		return Double.toString(a);
	}
	
	public double add(ArrayList<Double> l){
		double d = 0;
		for (int i = 0; i < l.size(); i++) {
			d = d + l.get(i);
		}
		return d;
	}

	
	
}
